package cadbury;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author George
 */
public class Command extends Thread {

    ArrayList input = new ArrayList();
    ArrayList commands = new ArrayList();
    ArrayList split = new ArrayList();
    String ask = "";
    String respond = "";
    String command;
    public Command(String command) throws IOException, ClassNotFoundException {
        command = this.command;
    }

    @Override
    public void run() {
        start();
        StringTokenizer splitter = new StringTokenizer(command);
        while (splitter.hasMoreTokens()) {
            split.add(splitter.nextToken());
        }
        if (split.get(0).toString().toLowerCase().equals("insert")) {
            if (split.get(1).toString().toLowerCase().equals("command")) {
                try {
                    insertCommand();
                } catch (IOException ex) {
                    Logger.getLogger(Command.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Command.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        if (split.get(0).toString().toLowerCase().equals("execute")) {
            execute();
        }
        if (split.get(0).toString().toLowerCase().equals("check")) {
            check();
        }
        if (split.get(0).toString().toLowerCase().equals("search")) {
            try {
                search();
            } catch (MalformedURLException ex) {
                Logger.getLogger(Command.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Command.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        System.out.println("Complete, sir");
    }

    public void insertCommand() throws IOException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
        try {
            Connection con = null;
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cadbury", "root", "sub83ab");
            java.sql.Statement st = con.createStatement();
            for (int i = 0; i < split.toArray().length; i++) {
                if (split.get(i).toString().toLowerCase().equals("insert")) {
                    if (split.get(i + 1).toString().toLowerCase().equals("command")) {
                        for (int m = i + 2; m < split.toArray().length - i; m++) {
                            ask += " " + split.get(m + i).toString();
                        }
                    }
                }
            }
            split.clear();
            BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What would you like the command to do?");
            String response = bf.readLine();
            StringTokenizer splitterResp = new StringTokenizer(response);
            while (splitterResp.hasMoreTokens()) {
                split.add(splitterResp.nextToken());
            }
            for (int l = 0; l < split.toArray().length; l++) {
                respond += " " + split.get(l).toString();
            }
            st.executeUpdate("INSERT INTO command (ask,respond) VALUES (\'" + ask + "\', \'" + respond + "\')");

            con.close();
        } catch (Exception e) {
            System.out.println("Sorry Sir, I am unable to complete the task.");
            e.printStackTrace();
        }
    }

    public void execute() {
        split.clear();
        String execute = "";
        try {
            Connection con = null;
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cadbury", "root", "sub83ab");
            java.sql.Statement st = con.createStatement();
            for (int i = 0; i < split.toArray().length; i++) {
                for (int m = i + 1; m < split.toArray().length - i; m++) {
                    execute += " " + split.get(m + i).toString();
                }
            }
            st.executeQuery("SELECT * FROM command WHERE respond = \'" + execute + "\'");
            System.out.println("Executing command");
            con.close();
        } catch (Exception e) {
            System.out.println("Sorry Sir, I was unable to execute.");
            e.printStackTrace();
        }
    }

    public void check() {
        split.clear();

    }

    public void search() throws MalformedURLException, IOException {
        System.out.println("hi");
        URL facebook = new URL("http://www.facebook.com/");
        URLConnection yc = facebook.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
        }
        in.close();
    }
}
